{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T16:40:52.708981Z",
     "start_time": "2025-01-08T16:40:52.293734Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 重复数据处理\n",
    "\n",
    "duplicated()：检测重复行，并返回一个布尔 Series，表示每行是否是重复行（从第二次出现开始标记为 True）\n",
    "\n",
    "drop_duplicates()：删除重复行。默认保留第一次出现的重复行，删除后续的重复行。\n",
    "\n",
    "value_counts()：统计每行出现的次数。"
   ],
   "id": "7397b7f1838817bd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:40:52.718680Z",
     "start_time": "2025-01-08T16:40:52.709982Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj=pd.DataFrame({'data1':['a']*4+['b']*4,'data2':np.random.randint(0,4,8)})\n",
    "print(df_obj)"
   ],
   "id": "a718b27b1db1fb16",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  data1  data2\n",
      "0     a      2\n",
      "1     a      0\n",
      "2     a      1\n",
      "3     a      1\n",
      "4     b      1\n",
      "5     b      1\n",
      "6     b      2\n",
      "7     b      2\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:41:18.131857Z",
     "start_time": "2025-01-08T16:41:18.127339Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# duplicated() 返回布尔型Series 表示每行是否为重复行\n",
    "print(df_obj.duplicated())"
   ],
   "id": "215a6cd0cc22ae8a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3     True\n",
      "4    False\n",
      "5     True\n",
      "6    False\n",
      "7     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:42:33.009205Z",
     "start_time": "2025-01-08T16:42:33.002902Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# drop_duplicates()过滤重复行，默认判断全部列\n",
    "'''\n",
    "参数说明：\n",
    "subset：指定用于检测重复的列。\n",
    "\n",
    "keep：指定保留哪个重复行。\n",
    "    'first'（默认）：保留第一次出现的重复行。\n",
    "\n",
    "    'last'：保留最后一次出现的重复行。\n",
    "\n",
    "    False：删除所有重复行。\n",
    "'''\n",
    "print(df_obj.drop_duplicates())"
   ],
   "id": "845ea0ae8ce3831f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  data1  data2\n",
      "0     a      2\n",
      "1     a      0\n",
      "2     a      1\n",
      "4     b      1\n",
      "6     b      2\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:43:05.884655Z",
     "start_time": "2025-01-08T16:43:05.879858Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 可指定按某些列判断，data2有重复的就丢弃\n",
    "print(df_obj.drop_duplicates('data2'))"
   ],
   "id": "af24ee1edf9163cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  data1  data2\n",
      "0     a      2\n",
      "1     a      0\n",
      "2     a      1\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T17:00:58.922001Z",
     "start_time": "2025-01-08T17:00:58.916391Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用 value_counts()，统计每行出现的次数\n",
    "print(df_obj.value_counts())"
   ],
   "id": "27defd4fdbc41dcf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data1  data2\n",
      "a      1        2\n",
      "b      2        2\n",
      "       1        2\n",
      "a      0        1\n",
      "       2        1\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "根据map传入的函数对每行或每列进行转换",
   "id": "69c3bb99dbc13d15"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:52:29.038501Z",
     "start_time": "2025-01-08T16:52:29.033930Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#  Series根据map传入的函数对每行或每列进行转换\n",
    "ser_obj=pd.Series(np.random.randint(0,10,10))\n",
    "print(ser_obj)"
   ],
   "id": "e4339e8fb244e88f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    4\n",
      "1    2\n",
      "2    5\n",
      "3    2\n",
      "4    0\n",
      "5    1\n",
      "6    5\n",
      "7    1\n",
      "8    2\n",
      "9    1\n",
      "dtype: int32\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:52:52.167879Z",
     "start_time": "2025-01-08T16:52:52.164420Z"
    }
   },
   "cell_type": "code",
   "source": "print(ser_obj.map(lambda x:x**2))",
   "id": "ae730e2d8887a68a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    16\n",
      "1     4\n",
      "2    25\n",
      "3     4\n",
      "4     0\n",
      "5     1\n",
      "6    25\n",
      "7     1\n",
      "8     4\n",
      "9     1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "数据替换\n",
    "\n",
    "replace根据值的内容进行替换"
   ],
   "id": "4f51eb41e53e6058"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:54:14.922867Z",
     "start_time": "2025-01-08T16:54:14.919849Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#单个值替换单个值\n",
    "print(ser_obj.replace(1,-100))"
   ],
   "id": "4f33b95ac6c5b6c8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      4\n",
      "1      2\n",
      "2      5\n",
      "3      2\n",
      "4      0\n",
      "5   -100\n",
      "6      5\n",
      "7   -100\n",
      "8      2\n",
      "9   -100\n",
      "dtype: int32\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:54:24.212099Z",
     "start_time": "2025-01-08T16:54:24.207689Z"
    }
   },
   "cell_type": "code",
   "source": [
    " #多个值替换一个值\n",
    "print(ser_obj.replace([6,8],-100))"
   ],
   "id": "6b1bfa8d2857e45",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    4\n",
      "1    2\n",
      "2    5\n",
      "3    2\n",
      "4    0\n",
      "5    1\n",
      "6    5\n",
      "7    1\n",
      "8    2\n",
      "9    1\n",
      "dtype: int32\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T16:54:33.917646Z",
     "start_time": "2025-01-08T16:54:33.913674Z"
    }
   },
   "cell_type": "code",
   "source": [
    " #多个值替换多个值\n",
    "print(ser_obj.replace([4,7],[-100,-200]))"
   ],
   "id": "cef2ed3e2b21222c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -100\n",
      "1      2\n",
      "2      5\n",
      "3      2\n",
      "4      0\n",
      "5      1\n",
      "6      5\n",
      "7      1\n",
      "8      2\n",
      "9      1\n",
      "dtype: int32\n"
     ]
    }
   ],
   "execution_count": 12
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
